﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
using VO;
using BAL;

namespace BTL
{
    public partial class frmNhanVien : Form
    {
        VO.NhanVienVO _NhanvienVO;
        BAL.NhanVienBAL _NhanvienBAL;
        SqlConnection conn = new SqlConnection(Core.Connectionstring.Con.ToString());
        SqlDataAdapter da = new SqlDataAdapter();
        DataTable dt = new DataTable();
        SqlCommand cmd = new SqlCommand();
        public frmNhanVien()
        {
           
            InitializeComponent();
        }
     
       

        private void frmNhanVien_Load(object sender, EventArgs e)
        {

            bthienthi_Click(sender, e);
            Nhanvienbinding();         
        }
        private void data()
        {
            _NhanvienVO = new NhanVienVO();
            _NhanvienVO.manv = box_manv.Text;
            _NhanvienVO.tennv = box_tennv.Text;
            _NhanvienVO.diachi = box_diachi.Text;
        }

       

        private void btthem_Click(object sender, EventArgs e)
        {
            if (box_manv.Text == "" || box_tennv.Text == "")
            {
                MessageBox.Show("Không Được Để Trống", "Thông Báo!", MessageBoxButtons.OK);
            }
            else
            {  
                    data();
                    _NhanvienBAL = new NhanVienBAL();
                    _NhanvienBAL._nhanvien_them(_NhanvienVO);
                    dataGridViewNhanVien.DataSource = dt;
                    MessageBox.Show("Thêm Nhân Viên Thành Công!", "Thông Báo!", MessageBoxButtons.OK);
                    conn.Close();
                    
            }
           }
        
       private void Nhanvienbinding()
        {
            box_manv.DataBindings.Clear();
            box_manv.DataBindings.Add("text", dataGridViewNhanVien.DataSource, "MaNV");
            box_tennv.DataBindings.Clear();
            box_tennv.DataBindings.Add("text", dataGridViewNhanVien.DataSource, "TenNV");
            box_diachi.DataBindings.Clear();
            box_diachi.DataBindings.Add("text", dataGridViewNhanVien.DataSource, "DiaChi");
        
           
        }

        private void bt_sua_Click(object sender, EventArgs e)
        {
            data();
            _NhanvienBAL = new NhanVienBAL();
            _NhanvienBAL._nhanvien_sua(_NhanvienVO);
            dataGridViewNhanVien.DataSource = dt;
            MessageBox.Show("Sửa Thông Tin Nhân Viên Thành Công!", "Thông Báo", MessageBoxButtons.OK);
            conn.Close();
            
        }


       

        private void bthienthi_Click(object sender, EventArgs e)
        {
            
            SqlCommand cmd = new SqlCommand();
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataTable dt = new DataTable();
            cmd.Connection = conn;
            conn.Open();
            cmd.CommandType = CommandType.Text;
            cmd.CommandText = "select * from NhanVien";

            cmd.ExecuteNonQuery();
            da.Fill(dt);
            dataGridViewNhanVien.DataSource = dt;
            conn.Close();
            

        }

        private void bt_xoa_Click_1(object sender, EventArgs e)
        {
            data();
            _NhanvienBAL._nhanvien_xoa(_NhanvienVO);
            dataGridViewNhanVien.DataSource = dt;
            MessageBox.Show("Đã Xóa Nhân Viên!", "Thông Báo!", MessageBoxButtons.OK);
            conn.Close();
            
        }

        private void bt_timkiem_Click(object sender, EventArgs e)
        {
            if (comboBox1.Text == "Mã nhân viên")
            {
                SqlCommand cmd = new SqlCommand();
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                DataTable dt = new DataTable();
                cmd.Connection = conn;
                conn.Open();
                cmd.CommandType = CommandType.Text;
                cmd.CommandText = "select * from NhanVien where maNV like '%" + textBox1.Text.Trim() + "%'";
                cmd.ExecuteNonQuery();
                da.Fill(dt);
                dataGridViewNhanVien.DataSource = dt;
                conn.Close();
            }

            if (comboBox1.Text == "Tên nhân viên")
            {
                SqlCommand cmd = new SqlCommand();
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                DataTable dt = new DataTable();
                cmd.Connection = conn;
                conn.Open();
                cmd.CommandType = CommandType.Text;
                cmd.CommandText = "select * from NhanVien where tenNV like '%" + textBox1.Text.Trim() + "%'";
                cmd.ExecuteNonQuery();
                da.Fill(dt);
                dataGridViewNhanVien.DataSource = dt;
                conn.Close();
            }
            
        }

        private void bt_thoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void dataGridViewNhanVien_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyData == Keys.F1)
            {
                frmHelp _help = new frmHelp();
                _help.Show();
            }
        }

        private void buttonX2_Click(object sender, EventArgs e)
        {
                frmHelp _help = new frmHelp();
                _help.Show();

        }

   
    }
}
